#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef int ElementType;

struct QueueRecord;
typedef struct QueueRecord *Queue;
//队列结构体。与Figure3.57相比，取消了Size分量
struct QueueRecord
{
    int Capacity; //队列容量
    int Front; //队头在数组中的下标
    int Rear; //队尾在数组中的下标
    ElementType *Array;//指向存储空间的指针
};

//如果队列为空，返回1，否则返回0
int IsEmpty(Queue Q){
   return Q->Front == Q->Rear;

}
//如果队列满，返回1，否则返回0
int IsFull(Queue Q){
    return (Q->Rear+1) % Q->Capacity == Q->Front;

}
//创建队列，返回指向QueueRecord结构体的指针
Queue CreateQueue(int MaxElements){
    Queue Q = (Queue)malloc(sizeof (struct QueueRecord));
    Q->Capacity = ++MaxElements;
    Q->Front = Q->Rear = 0;
    Q->Array = (ElementType *)malloc(MaxElements * sizeof(ElementType));
    return Q;
}
//销毁队列
void DisposeQueue(Queue Q){
    free(Q->Array);
    free(Q);
}
//清空队列
void MakeEmpty(Queue Q){
    Q->Front = Q -> Rear = 0;
}
//入队(加入队尾)
void Enqueue(ElementType X, Queue Q){
    if(IsFull(Q)){
        printf("Queue is full.\n");
        return;
    }
    Q->Array[Q->Rear] = X;
    Q->Rear = (Q->Rear +1) % Q->Capacity;
}
//返回队头元素
ElementType Front(Queue Q){
    if(IsEmpty(Q)){
        printf("Queue is empty.\n");
        return -1;
    }
    return Q->Array[Q->Front];
}
//出队(删除队头元素)
void Dequeue(Queue Q){
    if(IsEmpty(Q)){
        printf("Queue is empty.\n");
        return ;
    }
    Q->Front = (Q->Front+1) % Q->Capacity;
}
//删除队头元素，并将该元素返回
ElementType FrontAndDequeue(Queue Q){
    if(IsEmpty(Q)){
        printf("Queue is empty.\n");
        return -1;
    }
    ElementType temp = Q->Array[Q->Front];
    Q->Front = (Q->Front+1) % Q->Capacity;
    return temp;
}
//返回队列长度(队列中有效元素的个数)
int Size(Queue Q){
    return (Q->Rear-Q->Front+Q->Capacity)%Q->Capacity;
}
//从0号位置开始输出数据区的所有元素。无效位置输出'X'
void show(Queue Q);



int main(void)
{
    char cmd[20];
    Queue Q = CreateQueue(9);
    int x;
    scanf("%s", cmd);
    while (strcmp(cmd, "END") != 0)
    {
        if (strcmp(cmd, "ENQUEUE") == 0)
        {
            if (IsFull(Q) == 1)
            {
                printf("FULL\n");
            }
            else
            {
                scanf("%d", &x);
                Enqueue(x, Q);
            }
        }
        if (strcmp(cmd, "DEQUEUE") == 0)
        {
            if (IsEmpty(Q) == 1)
            {
                printf("EMPTY\n");
            }
            else
            {
                Dequeue(Q);
            }
        }
        if (strcmp(cmd, "GETFRONT") == 0)
        {
            x = Front(Q);
            printf("%d\n", x);
        }
        if (strcmp(cmd, "SIZE") == 0)
        {
            printf("SIZE = %d\n", Size(Q));
        }
        if (strcmp(cmd, "SHOW") == 0)
        {
            show(Q);
        }
        scanf("%s", cmd);
    }
    DisposeQueue(Q);
    return 0;
}

//从0号位置开始输出数据区的所有元素。无效位置输出'X'
void show(Queue Q)
{
    if (Q->Front == Q->Rear)
        return;
    if (Q->Front < Q->Rear)
    {
        for (int i = 0; i < Q->Front; i++)
        {
            printf("X ");
        }
        for (int i = Q->Front; i < Q->Rear; i++)
        {
            printf("%d ", Q->Array[i]);
        }
        for (int i = Q->Rear; i < Q->Capacity; i++)
        {
            printf("X ");
        }
    }
    else
    {
        for (int i = 0; i < Q->Rear; i++)
        {
            printf("%d ", Q->Array[i]);
        }
        for (int i = Q->Rear; i < Q->Front; i++)
        {
            printf("X ");
        }
        for (int i = Q->Front; i < Q->Capacity; i++)
        {
            printf("%d ", Q->Array[i]);
        }
    }
    printf("\n");
}

/* 请在这里填写答案 */
